OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//员工考勤管理
OB.Emes_ActionButton.attendanceButton  = function(d, b) {
	var e = b.activeView.viewGrid;
	var a = e.getSelectedRecords();
	var c = a.getLength();
	if (c) {
		// 选择纪录行
		var selectRecords = [];
		for (var i = 0; i < c; i++) {
			selectRecords.push(a[i].id);
			a.hz_id
		}
		OB.Emes_ActionButton.MyRefresh=function(){
			b.refresh();
		}
		

		usercodes = a[0].attendance;
		//usernames = a[0].user$_identifier;
		usernames =  a[0]._identifier;
		
		OB.RemoteCallManager
				.call(													      
						"com.mes.handler.emplooyAttendanceHandler",
						{
							ids : selectRecords.join("#####")
						},
						{},
						function(rpcResponse, data, rpcRequest) {
							if (!data.result) {
								alert(data.msg);
								return;
							}
							refershView = b;
							isc.External_AttendanceButton.create(data).show();
						});
	} else {
		isc.showMessage("请选择考勤记录！");
	}
};

isc.defineClass('External_AttendanceButton',isc.OBPopup);

isc.External_AttendanceButton.addProperties({

	width : "70%",
	height : "70%",
	title : null,
	showMinimizeButton : false,
	showMaximizeButton : false,

	callPutData : null,
	callPutForm : null,
	okButton : null, 
	cancelButton : null,

	initWidget : function(data) {
		this.createattendanceGrid();
		
		var mayDate = new Date();
		
		var year = mayDate.getFullYear();  //获取当前年份
		
		var month = mayDate.getMonth()+1;   //获取当前月份（0-11，0代表一月，加1后就是正常月份）
		
		now = new Date(year , month,0);
		
		var daycount = now.getDate(); //获取当前月份的天数
		
		//var usercode = data.AttendanceList[0].usercode; 

		//var username = data.AttendanceList[0].username; 
		
		var list = [];
		for (var i = 0;i < data.AttendanceList.length;i ++) {
			list[data.AttendanceList[i].dates.substring(8,10) * 1 - 1] = data.AttendanceList[i];
		}
		for (var i = 0;i < daycount;i ++) {
			if (!list[i]) {
				//list[i] = {dates: "2018-06-" + (i + 1)};
				list[i] = {dates: year+"-"+month+"-" + (i + 1),usercode : usercodes,username : usernames};
			}
		}
		this.setGridValue(list, this.callPutForm);
		

		this.createUserGroupPanel();
	

		this.Super('initWidget',arguments);
		
	},
	createattendanceGrid: function() {
		var _self = this;
		this.callPutForm = isc.ListGrid.create({
			
				title: "员工考勤记录",
				ID: "attendanceGrid",
				width: '30%',
				height: this.height,
				fields: [
					{width: '25%', title: '日期', name: 'dates',canEdit: false},
					{width: '15%', title: "员工考勤号码", name: "usercode",canEdit: false},
					{width: '20%', title: '员工姓名', name: 'username',canEdit: false},
					{width: '25%',type: 'select',title:'班次类型',	  name: 'classtype', valueMap: {Rests: '休息', Lates: '迟到', Earlyretreat: '早退', Lateandearly: '迟到加早退', Dayshifts: '白班', Eveningshifts: '晚班' ,abnormal: '异常' , Vacation :'休假'} , readOnlyDisplay:"static",startRow:true},
					//{width: '25%', title: '班次类型', name: 'classtype'},
					{width: '35%', title: '上班打卡时间', name: 'gotime',type : 'datetime'},
					{width: '35%', title: '下班打卡时间', name: 'outtime',type : 'datetime'},
					{width: '25%',type: 'select',title:'上班未打卡', name: 'dakaiy', valueMap: {Y: '是', } , readOnlyDisplay:"static",startRow:true},
					{width: '25%',type: 'select',title:'下班未打卡', name: 'dakain', valueMap: {Y: '是', } , readOnlyDisplay:"static",startRow:true},
					//{width: '25%', title: '上班未打卡', name: 'dakaiy',type : 'text'},
					//{width: '25%', title: '下班未打卡', name: 'dakain',type : 'text'},
					//{width: '25%', title: '休假类型', name: 'xiujia',type : 'text'},
					{width: '25%',type: 'select',title:'休假类型',	  name: 'xiujia', valueMap: {holiday: '婚假', sickleave: '病假', Bereavement: '丧假', Absenteeism: '旷工', business: '出差', industrial: '工伤',privateleave:'事假' } , readOnlyDisplay:"static",startRow:true},
					{width: '25%', title: '休假时长', name: 'xiujiatime',type : 'text'},
					{width: '25%', title: '备注', name: 'remarks',type : 'text'},
					{width: '25%', title: '考勤ID', name: 'kqid',type : 'text'}
					//,{title: '额定工时(m)', name: 'time', canEdit: false}
				],
				selectionType: "single",
				canEdit: true,
				autoFetchData: false,
				autoSaveEdits: true,
				alternateRecordStyles: true,
				canDragRecordsOut: false,
				canAcceptDroppedRecords: false,
				canReorderRecords: true,
				canReorderFields: false,
				canResizeFields: true,
				recordClick: function(grid, data, index) {
					// _self.routeData = data;
					//_self.serchData(data.id, "resource");
					//_self.setValue(data, _self.resourcePanelForm);
				}	
		});
		// 隐藏ids
		attendanceGrid.hideField('kqid');
	   },
	   createUserGroupPanel: function() {
			var _self = this;
			
			// OK Button
			this.okButton = isc.OBFormButton.create({
						title : '确定',
						popup : this,
						action : function() {
							
							attendanceGrid.saveEdits();
							
							var upResult = attendanceGrid.data;
							var cL = upResult.length;
							
							
							for (var i = 0; i < cL; i++) {
								var gotime = upResult[i].gotime; 
								var outtime = upResult[i].outtime; 
								
								var dates = upResult[i].dates; 
								
								var classtype = upResult[i].classtype; //班次类型
								
								
								
								var xiujia = upResult[i].xiujia;  //休假类型
								
								var xiujiatime = upResult[i].xiujiatime; //休假时长
								
								
								if(xiujia && !xiujiatime){
									isc.showMessage("已选择休假类型，请选择"+dates+"日休假时长！");
									return;
								}
								
								if(!xiujia && xiujiatime){
									isc.showMessage("已选择休假时长，请选择"+dates+"日休假类型！");
									return;
								}
								
								//判断休假时长是否为数字
								if(xiujiatime){
								    if(!(/^[0-9]+([.]{1}[0-9]+){0,1}$/.test(xiujiatime))){
								 		_self.$options.methods.vueAlert("休假时长填写错误！");
										return;
								 	}
								}
								
								
								  //比较上班打卡时间和下班打卡时间  
								 /* if(gotime !="" && outtime !="" && gotime.getTime() >=outtime.getTime()) {    
									  alert(dates+"日上班打卡时间不能大于下班打卡时间！");    
								  return false;    
								 }  */
								
								
							}
							
								
							var callback = function(
									rpcResponse,
									data,
									rpcRequest) {
								if (data.message.severity == "success") {
									refershView.refresh();
									isc.showMessage(data.message.text,isc.OBMessageBar.TYPE_INFO);
									rpcRequest.clientContext.popup.closeClick();
								} else {
									isc.showMessage(data.message,isc.OBMessageBar.TYPE_INFO);
								}
								OB.Emes_ActionButton.MyRefresh();

							};
							OB.RemoteCallManager.call("com.mes.handler.AttendanceSavebuttonHandler",{dateParam : upResult}, {}, callback);
							this.popup.closeClick();
						}
					});

			// Cancel Button
			this.cancelButton = isc.OBFormButton.create({
						title : '取消',
						popup : this,
						action : function() {
							this.popup.closeClick();
						}
					});

			//Add the elements into a layout   
			this.items = [ isc.VLayout.create({
				defaultLayoutAlign : "center",
				align : "center",
				width : "100%",
				layoutMargin : 15,
				membersMargin : 70,
				members : [
						isc.HLayout
								.create({
									defaultLayoutAlign : "center",
									align : "center",
									width : "100%",
									height : "80%",
									members : [ this.callPutForm ]
								}),
						isc.HLayout
								.create({
									defaultLayoutAlign : "center",
									align : "center",
									membersMargin : 10,
									marginBottom : 10,
									members : [
											this.okButton,
											this.cancelButton ]
								}) ]
			}) ];
			
		},
	setGridValue: function(data, grid){
		var rows = grid.data;
		for (var i = 0;i < rows.length;i ++) {
			grid.removeData(rows[i]);
		}
		grid.setData(data);
	},


});
